package com.Prot2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.Prot2.R;
import com.Prot2.Logger.CoordinatorContinuous;
import com.Prot2.Logger.CoordinatorFile;

public class LoggerFileUI extends Activity{
	
	private static final String TAG = "LoggerUI";
	private Button buttonToggleLogging, buttonBackToMain;
	private Spinner spinner_location;
	private TextView text_loggingActive;
	private ProgressBar progress_loggingActive;
	
	
	public void setViews(){
setContentView(R.layout.logger_file);
		
		buttonToggleLogging = (Button) findViewById(R.id.button_ToggleLogging);
		buttonBackToMain = (Button) findViewById(R.id.button_BackToMain);
		
		text_loggingActive = (TextView) findViewById(R.id.logger_file_text_logging_active);
		progress_loggingActive = (ProgressBar) findViewById(R.id.logger_file_progress_logging_active);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.location_tags));
		spinner_location = (Spinner) findViewById(R.id.spinner_location);
		spinner_location.setAdapter(adapter);
		spinner_location.setSelection(Globals.currentLocationTag);
		
		spinner_location.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
				//Globals.currentLocationTag = (String) parent.getItemAtPosition(pos);
				Globals.currentLocationTag = pos;
				Toast.makeText(getApplicationContext(), "Selected: " + (String)parent.getItemAtPosition(pos), Toast.LENGTH_SHORT).show();
			}
			public void onNothingSelected(AdapterView<?> parent){
				
			}
		});
		
		if(Globals.gathererIsLogging){
			text_loggingActive.setVisibility(View.VISIBLE);
			progress_loggingActive.setVisibility(View.VISIBLE);
		}else{
			text_loggingActive.setVisibility(View.INVISIBLE);
			progress_loggingActive.setVisibility(View.INVISIBLE);
		}
		
		buttonToggleLogging.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				//startLogging
				if(!Globals.gathererIsLogging){
					v.getContext().startService(new Intent(v.getContext(),CoordinatorFile.class));
					Globals.gathererIsLogging=true;
					Log.w(TAG,"called start Logging");
					text_loggingActive.setVisibility(View.VISIBLE);
					progress_loggingActive.setVisibility(View.VISIBLE);
				}
				else{
					v.getContext().stopService(new Intent(v.getContext(),CoordinatorFile.class));
					Globals.gathererIsLogging=false;
					text_loggingActive.setVisibility(View.INVISIBLE);
					progress_loggingActive.setVisibility(View.INVISIBLE);
					finish();
				}

			}
		});
			
		buttonBackToMain.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				//Back to Main
				v.getContext().startActivity(new Intent(v.getContext(),MainActivity.class));
				Log.w(TAG,"called back to Main");
				}	
		});
		
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setViews();
	}
	
	@Override
	public void onRestart(){
		super.onRestart();
		
		setViews();
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
		stopService(new Intent(getApplicationContext(),CoordinatorFile.class));
		Globals.gathererIsLogging = false;
	}
	
}
